/*
 * This project borrows code from these contributors:
 * Salamijack - HookShot - https://github.com/dggriffin/HookShot
 * 
 * 
 * 
 * 
 * 
 * 
 */


package HanzVu.superplugin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Logger;

import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.Event.Priority;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

//Has the permission definitions
import com.nijiko.permissions.PermissionHandler;
import com.nijikokun.bukkit.Permissions.Permissions;

public class superplugin extends JavaPlugin {
    //Set up logger
    private final Logger log = Logger.getLogger("Minecraft");
    
    //Create all of our callback objects
    private final SPPlayerListener playerListener = new SPPlayerListener(this);
    private final SuPBlockListener blockListener = new SuPBlockListener(this);
    private final SPEntityListener entityListener = new SPEntityListener(this);
    private final HashMap<Player, Boolean> debugees = new HashMap<Player, Boolean>();
    public final HashMap<Player, ArrayList<Block>> SPUsers = new HashMap<Player, ArrayList<Block>>();
    public static PermissionHandler permissionHandler;
    
    //permission info
    private Permissions permissions;
    
    
    
    @Override
    public void onDisable() {
        System.out.println("Goodbye world!");
    }

    @Override
    public void onEnable() {
        //Register Events
        //I hightly doubt all of these are necessary, weeding will take place later
        PluginManager pm = getServer().getPluginManager();
        pm.registerEvent(Event.Type.PLAYER_JOIN, playerListener, Priority.Normal, this);
        pm.registerEvent(Event.Type.PLAYER_QUIT, playerListener, Priority.Normal, this);
        pm.registerEvent(Event.Type.PLAYER_MOVE, playerListener, Priority.Normal, this);
        pm.registerEvent(Event.Type.BLOCK_PHYSICS, blockListener, Priority.Normal, this);
        pm.registerEvent(Event.Type.BLOCK_CANBUILD, blockListener, Priority.Normal, this);
        pm.registerEvent(Event.Type.BLOCK_BREAK, blockListener, Priority.Normal, this);
        pm.registerEvent(Event.Type.PLAYER_TOGGLE_SNEAK, playerListener, Priority.Normal, this);
        pm.registerEvent(Event.Type.PLAYER_ITEM_HELD, playerListener, Priority.Normal, this);
        pm.registerEvent(Event.Type.PLAYER_INTERACT, playerListener, Priority.Normal, this);
        pm.registerEvent(Event.Type.ENTITY_DAMAGE, entityListener, Priority.Highest, this);
                
                

        // Register our command
        //getCommand("pos").setExecutor(new SamplePosCommand(this));
        //getCommand("debug").setExecutor(new SampleDebugCommand(this));
        
        SPPermissions.initialize(getServer());
        // EXAMPLE: Custom code, here we just output some info so we can check all is well
        PluginDescriptionFile pdfFile = this.getDescription();
        System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
    }

    public boolean isDebugging(final Player player) {
        if (debugees.containsKey(player)) {
            return debugees.get(player);
        } else {
            return false;
        }
    }

    public void setDebugging(final Player player, final boolean value) {
        debugees.put(player, value);
    }
    
    public boolean enabled(Player player)
    {
        return this.SPUsers.containsKey(player);
    }
    
    public void setupPermissions()
    {
        Plugin permissionsPlugin = this.getServer().getPluginManager().getPlugin("Permissions");
        if(this.permissionHandler == null)
        {
          if(permissionsPlugin != null)
          {
              this.permissionHandler = ((Permissions) permissionPlugin).getHandler();
          }
          else
          {
              log.info("Permission system not detected, defaulting to OP");
          }
        }
    }
    
    public Permissions getPermission()
    {
        return permissions;
    }
    
}